#include "sys.h"

CSys::CSys() : m_App(NULL)
			 , m_TSysTask(NULL){
	
}

CSys::~CSys(){
	if(m_App)
		delete m_App;
}

void CSys::initialize(){
	g_User.load_user_info();
	g_Config.load_config_info();
	m_App = new CApp;
	m_TSysTask = new TSysTask;
	m_App->initialize();
}

void CSys::run(){
	m_App->run();
}

bool CSys::get_app_state(){
	return m_App->is_running();
}

void CSys::suspend(){
	m_App->suspend();
}

void CSys::save(){
	XMLNode xMainNode = XMLNode::createXMLTopNode("Task",TRUE);
	XMLNode child;
	for(TSysTaskIter iter = m_TSysTask->begin(); iter != m_TSysTask->end(); ++iter){
		if((*iter).m_Type){
			child = xMainNode.addChild("Dir");
		}
		else{
			child = xMainNode.addChild("File");
		}
		if((*iter).m_Action){
			child.addAttribute("Action","Del");
		}
		else{
			child.addAttribute("Action","Add");
		}
		child.addAttribute("Size", ltoa((*iter).m_Size));
	}
	char tempbuf[20];
	_tzset();
	_strtime(tmpbuf);
	string FileName;
	FileName = tempbuf;
	tempbuf[20] = "";
	_strdate(tempbuf);
	FileName += tempbuf;
	xMainNode.writeToFile(FileName.c_str());
}

void CSys::add_file(char * n_fileName, unsigned long n_fileSize){
	m_SSysTask.m_Action = 0;
	m_SSysTask.m_Type = 0;
	m_SSysTask.m_Name = n_fileName;
	m_SSysTask.m_Size = n_fileSize;
	m_TSysTask->push_back(m_SSysTask);
}

void CSys::add_dir(char * n_dirName, unsigned long n_dirSize){
	m_SSysTask.m_Action = 0;
	m_SSysTask.m_Type = 1;
	m_SSysTask.m_Name = n_dirName;
	m_SSysTask.m_Size = n_dirSize;
	m_TSysTask->push_back(m_SSysTask);
}

void CSys::del_file(char * n_fileName){
	m_SSysTask.m_Action = 1;
	m_SSysTask.m_Type = 0;
	m_SSysTask.m_Name = n_fileName;
	m_SSysTask.m_Size = 0;
	m_TSysTask->push_back(m_SSysTask);
}

void CSys::del_dir(char * n_dirName){
	m_SSysTask.m_Action = 1;
	m_SSysTask.m_Type = 1;
	m_SSysTask.m_Name = n_dirName;
	m_SSysTask.m_Size = 0;
	m_TSysTask->push_back(m_SSysTask);
}
